package com.yy.jhl.ui.manager.activity;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.KeyBordUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.CollectInfo;
import com.yy.jhl.bean.ProjectInfo;
import com.yy.jhl.interfaces.MessageNumCallBack;
import com.yy.jhl.ui.manager.adapter.ProjectAdapter;
import com.yy.jhl.ui.manager.contract.MenuOneContract;
import com.yy.jhl.ui.manager.presenter.MenuOnePresenter;
import com.yy.jhl.ui.qrcode.QRScanActivity;
import com.yy.jhl.util.DataRequestUtil;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

/**
 * Created by libingn on 2017/12/4.
 */

public class CollectListActivity extends BaseActivity<MenuOnePresenter> implements MenuOneContract.View, AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.order_pull_list_view)
    PullToRefreshListView orderPullListView;
    ListView orderListView;
    ProjectAdapter projectAdapter;
    @BindView(R.id.no_data_parent)
    LinearLayout noDataParent;
    private int currentPage = 1;

    @Override
    public int getLayoutId() {
        return R.layout.act_collect_list;
    }

    @Override
    public void initView() {
        //注册EventBus
        EventBus.getDefault().register(this);
        orderPullListView.setMode(PullToRefreshBase.Mode.BOTH);
        orderListView = orderPullListView.getRefreshableView();
        orderListView.setDividerHeight(3);
        projectAdapter = new ProjectAdapter(mContext,true);
        orderListView.setAdapter(projectAdapter);
        orderPullListView.setOnItemClickListener(this);
        orderPullListView.setOnRefreshListener(this);
        showTitle("我的收藏", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        mPresenter.getCollectList(currentPage);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ProjectInfo.DataBean projectInfo = (ProjectInfo.DataBean) projectAdapter.getItem(i - 1);
        projectInfo.setRelated("1");
        ProjectDetailActivity.startAction(mContext, projectInfo);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        projectAdapter.clearData();
        currentPage = 1;
        mPresenter.getCollectList(currentPage);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        currentPage += 1;
        mPresenter.getCollectList(currentPage);

    }

    @Override
    public void getProjectSuccess(ProjectInfo projectInfo) {
        if (currentPage == 1){
            projectAdapter.clearData();
            if (projectInfo.getData() == null ||projectInfo.getData().size()== 0){
                noDataParent.setVisibility(View.VISIBLE);
                orderPullListView.setVisibility(View.GONE);
            }else {
                noDataParent.setVisibility(View.GONE);
                orderPullListView.setVisibility(View.VISIBLE);
            }
        }
        orderPullListView.onRefreshComplete();
        projectAdapter.addData(projectInfo.getData());

    }

    @Override
    public void getProjectListFail() {
        orderPullListView.onRefreshComplete();
        noDataParent.setVisibility(View.VISIBLE);
        orderPullListView.setVisibility(View.GONE);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //反注册EventBus
        EventBus.getDefault().unregister(this);
    }
    public void onEventMainThread(CollectInfo collectInfo) {
        if ("0".equals(collectInfo.getRelateId())){
            projectAdapter.removeByProjectId(collectInfo.getProjectId());
            if (projectAdapter.getCount() == 0){
                noDataParent.setVisibility(View.VISIBLE);
                orderPullListView.setVisibility(View.GONE);
            }
        }
    }
}
